<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>用正则表达式将字符串分割到数组中</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.split" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">split</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">split</span> &mdash; <span class="dc-title">用正则表达式将字符串分割到数组中</span></p>

 </div>
 <div class="refsect1 unknown-unknown-unknown-unknown-unknown-seealst" id="refsect1-function.split-unknown-unknown-unknown-unknown-unknown-seealst">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>split</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$pattern</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$limit</code></span>
  ] ) : <span class="type">array</span></div>

  <div class="tip"><strong class="tip">Tip</strong>
   <p class="para">
    <span class="function"><a href="preg_split.html" class="function">preg_split()</a></span> 函数使用了
    Perl 兼容正则表达式语法，通常是比
    <span class="function"><strong>split()</strong></span> 更快的替代方案。如果不需要正则表达式的威力，则使用
    <span class="function"><a href="explode.html" class="function">explode()</a></span> 更快，这样就不会招致正则表达式引擎的浪费。
   </p>
  </div>
  <p class="para">
   本函数返回一个字符串数组，每个单元为
   <code class="parameter">string</code> 经区分大小写的正则表达式
   <code class="parameter">pattern</code> 作为边界分割出的子串。如果设定了
   <code class="parameter">limit</code>，则返回的数组最多包含
   <code class="parameter">limit</code> 个单元，而其中最后一个单元包含了
   <code class="parameter">string</code> 中剩余的所有部分。如果出错，则
   <span class="function"><strong>split()</strong></span> 返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
   将 <var class="filename">/etc/passwd</var> 中的前四个字段分割出来：
  </p>
  <p class="para">
   <div class="example" id="example-5887">
    <p><strong>Example #1 <span class="function"><strong>split()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$uid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$gid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$extra</span><span style="color: #007700">)&nbsp;=<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">split&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">":"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$passwd_line</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="simpara">
   如果字符串中有 <span class="replaceable">n</span> 个与
   <code class="parameter">pattern</code> 匹配的项目，则返回的数组将包含
   <em><span class="replaceable">n</span>+1</em> 个单元。例如，如果没有找到
   <code class="parameter">pattern</code>，则会返回一个只有一个单元的数组。当然，如果
   <code class="parameter">string</code> 为空也是这样。
  </p>
  <p class="para">
   解析可能用斜线，点，或横线分割的日期：
  </p>
  <p class="para">
   <div class="example" id="example-5888">
    <p><strong>Example #2 <span class="function"><strong>split()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;分隔符可以是斜线，点，或横线<br /></span><span style="color: #0000BB">$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"04/30/1973"</span><span style="color: #007700">;<br />list(</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$day</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">split&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">'[/.-]'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Month:&nbsp;</span><span style="color: #0000BB">$month</span><span style="color: #DD0000">;&nbsp;Day:&nbsp;</span><span style="color: #0000BB">$day</span><span style="color: #DD0000">;&nbsp;Year:&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   想仿效 Perl 中类似的 <strong class="command">@chars =
   split(&#039;&#039;, $str)</strong> 行为，请参考
   <span class="function"><a href="preg_split.html" class="function">preg_split()</a></span> 或 <span class="function"><a href="str_split.html" class="function">str_split()</a></span> 函数中的例子。
  </p>
  <p class="para">
   注意 <code class="parameter">pattern</code>
   是一个正则表达式。如果想要用的分割字符是正则表达式中的特殊字符，要先将其转义。如果觉得
   <span class="function"><strong>split()</strong></span>（或其它任何 regex 函数）行为古怪的话，请阅读包含在
   PHP 发行包中 <var class="filename">regex/</var> 子目录下的
   <var class="filename">regex.7</var> 文件。该文件是手册页面格式，可以用类似
   <strong class="command">man /usr/local/src/regex/regex.7</strong> 的命令来阅读。
  </p>
  <p class="para">
   参见 <span class="function"><a href="preg_split.html" class="function">preg_split()</a></span>，<span class="function"><a href="spliti.html" class="function">spliti()</a></span>，<span class="function"><a href="str_split.html" class="function">str_split()</a></span>，<span class="function"><a href="explode.html" class="function">explode()</a></span>，<span class="function"><a href="implode.html" class="function">implode()</a></span>，<span class="function"><a href="chunk_split.html" class="function">chunk_split()</a></span>
   和 <span class="function"><a href="wordwrap.html" class="function">wordwrap()</a></span>。
  </p>
 </div>

</div></div></div></body></html>